/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-06-28 13:52:25
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-01-10 15:52:59
 */
import { Component, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { BomContentServiceProxy, BomEditCopyDto, MaterialsEditContentDto, CommonServiceProxy, MaterialsServiceProxy, MakeTaskServiceProxy } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
//查看履历
import { ShowResumeComponent } from './show-resume/show-resume.component';
import { AddOrEditPathComponent } from './add-or-edit-path/add-or-edit-path.component';
import { BomLookComponent } from './bom-look/bom-look.component';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { MessageService } from '@abp/message/message.service';
@Component({
    selector: 'app-process-path',
    templateUrl: './process-path.component.html',
    styleUrls: ['./process-path.component.less']
})
export class ProcessPathComponent extends AppComponentBase {
    @ViewChild('showResume') ShowResume: ShowResumeComponent;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('paginator') importPaginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('dataTable') importTable: Table;
    @ViewChild('addOrEditPath') addOrEditPath: AddOrEditPathComponent;
    @ViewChild('bomLook') bomLook: BomLookComponent;
    model: string = '';
    lotNo: string = '';
    drawingNo: string = '';
    deleverTiems: string = '';
    title: string = '';
    rowdata: any;
    selectImport: any;
    importBom = false;
    bomStatus = undefined;
    selectRow: any;
    importData = [];
    saveFlag = true;
    addParent = false;
    addForm = {
        materialCode: '', materialname: '', materialProperties: 0,
        materialModel: undefined, sendTime: undefined, isUsed: true, remark: ''
    };
    modelOptions = [];
    sendTimeOptions = [];
    materialPropertiesOption = [
        {
            label: '请选择',
            value: ''
        }, {
            label: '整装',
            value: 0
        }, {
            label: '芯级',
            value: 4
        }, {
            label: '部件',
            value: 1
        }, {
            label: '组件',
            value: 2
        }, {
            label: '零件',
            value: 3
        },
    ];
    constructor(
        injector: Injector,
        private _bomContentServiceProxy: BomContentServiceProxy,
        private _fileDownloadService: FileDownloadService,
        private _messageService: MessageService,
        private _commonServiceProxy: CommonServiceProxy,
        private _materialsServiceProxy: MaterialsServiceProxy,
        private _makeTaskServiceProxy: MakeTaskServiceProxy,
    ) {
        super(injector);
    }
    addSave() {
        let input = new MaterialsEditContentDto();
        input.materialNo = this.addForm.materialCode;
        input.materialName = this.addForm.materialname;
        input.materialProperty = this.addForm.materialProperties;
        input.modelId = this.addForm.materialModel;
        input.sendTimeId = this.addForm.sendTime;
        input.remark = this.addForm.remark;
        if (this.addForm.isUsed) {
            input.isUsed = 1;
        } else {
            input.isUsed = 0;
        }
        if (this.saveFlag) {
            this.saveFlag = false;
            this._materialsServiceProxy.createOrUpdateMaterialsContent(input)
                .pipe(finalize(() => { }))
                .subscribe((result) => {
                    this.saveFlag = true;
                    if (result.code == 0) {
                        this._messageService.success('保存成功');
                        this.addForm.isUsed = true;
                        this.addForm.materialCode = undefined;
                        this.addForm.materialModel = undefined;
                        this.addForm.materialProperties = 0;
                        this.addForm.materialname = undefined;
                        this.addForm.remark = undefined;
                        this.addForm.sendTime = undefined;
                        this.addParent = false;
                        this.getTableList();
                    } else {
                        this._messageService.error(result.message)
                    }

                });
        }
    }
    add() {
        this.addParent = true;
    }
    onChange(event) {
        this.sendTimeOptions = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._commonServiceProxy.getLotNoList(this.addForm.materialModel)
            .pipe(finalize(() => { }))
            .subscribe(result => {
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item.label;
                    data.value = item.value;
                    this.sendTimeOptions.push(data);
                }
            });
    }
    getMaterialCode(event) {
        this._makeTaskServiceProxy.getJobNumber(
            this.addForm.sendTime,
            this.addForm.materialModel
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.addForm.materialname = result.productName;
                this.addForm.materialCode = result.drawingNo;
            });
    }
    closeParent(event) {
        this.sendTimeOptions = [];
        this.addForm.isUsed = true;
        this.addForm.materialCode = undefined;
        this.addForm.materialModel = undefined;
        this.addForm.materialProperties = 0;
        this.addForm.materialname = undefined;
        this.addForm.remark = undefined;
        this.addForm.sendTime = undefined;
        this.addParent = false;
    }
    ngOnInit() {
        this.getModelList();
    }
    getModelList() {
        //获取型号下拉
        this.modelOptions = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._commonServiceProxy.getModelList()
            .pipe(finalize(() => { }))
            .subscribe(result => {
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item.label;
                    data.value = item.value;
                    this.modelOptions.push(data);
                }
            });
    }
    export(row) {
        //导出
        this._bomContentServiceProxy.getBomContentToExcel(
            row.id
        ).pipe(finalize(() => { })).subscribe(result => {
            // this._fileDownloadService.downloadTempFile(result);
            window.open(result);
            URL.revokeObjectURL(result);
        });
    }
    // 获取表格
    getTableList(event?: LazyLoadEvent) {
        // if (this.primengTableHelper.shouldResetPaging(event)) {
        //     this.paginator.changePage(0);
        //     return;
        // }
        setTimeout(() => {
            abp.ui.setBusy();
            this._bomContentServiceProxy.getBomList(
                this.lotNo,
                this.model,
                this.drawingNo,
                this.deleverTiems,
                this.bomStatus,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                abp.ui.clearBusy();
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
        }, 5);
    }
    reset() {
        this.model = undefined;
        this.lotNo = undefined;
        this.drawingNo = undefined;
        this.bomStatus = undefined;
        this.deleverTiems = undefined;
    }
    addOrUpdate(row?: any) {
        this.rowdata = row;
        if (row == undefined) {
            this.title = 'Bom新增';
        } else {
            this.title = 'Bom维护';
        }
        setTimeout(() => {
            this.addOrEditPath.show();
        }, 10)
    }
    lookResume(row) {
        //履历
        this.rowdata = row;
        setTimeout(() => {
            this.ShowResume.show()
            this.ShowResume.getTableList();
        }, 20)
    }
    look(row) {
        //BOM查看
        this.rowdata = row;
        setTimeout(() => {
            this.bomLook.show();
        }, 20)
    }
    bomImport() {
        if (this.selectRow == undefined || this.selectRow == null) {
            this._messageService.error('请先选择一条数据!');
            return;
        }
        this.importBom = true;
        this.importData = [];
        this.getImportList();
    }
    getImportList() {
        this._bomContentServiceProxy.getBomModelList(
            this.selectRow.model,
            this.selectRow.sendTime
        ).pipe(finalize(() => { })).subscribe(result => {
            this.importData = result;
        });
    }
    importSave() {
        let input = new BomEditCopyDto();
        input.bomIdNew = this.selectRow.id;
        input.bomIdOld = this.selectImport.id;
        if (this.saveFlag) {
            this.saveFlag = false;
            this._bomContentServiceProxy.createBomCopy(
                input
            ).pipe(finalize(() => { }))
                .subscribe((result) => {
                    this.saveFlag = true;
                    if (result.code == 0) {
                        this._messageService.success('保存成功');
                        this.importBom = false;
                        this.getTableList();
                    } else {
                        this._messageService.error(result.message)
                    }
                });
        }

    }
}
